package mywebapps.security.dao.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import mywebapps.common.utils.HibernateUtil;
import mywebapps.security.dao.UserDao;
import mywebapps.security.model.Role;
import mywebapps.security.model.User;
import mywebapps.security.model.UserRole;

public class UserDaoImpl implements UserDao
{

    @Override
    public User getUser(String username)
    {
        SessionFactory factory = HibernateUtil.getInstance().getSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        Query query = session.getNamedQuery("User.findUserByUsername");
        query.setString("username", username);
        
        User user = (User) query.uniqueResult();
        
        session.getTransaction().commit();
        return user;
    }
    
    @Override
    public User getUser(int userId)
    {
        SessionFactory factory = HibernateUtil.getInstance().getSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        
        User user = (User) session.get(User.class, userId);
        
        session.getTransaction().commit();
        return user;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> getAllUsers()
    {
        SessionFactory factory = HibernateUtil.getInstance().getSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        
        Query query = session.getNamedQuery("User.findAllUsers");
        List<User> users = (List<User>) query.list();
        
        session.getTransaction().commit();
        return users;
    }
    
    @Override
    public void createUser(User user)
    {
        SessionFactory factory = HibernateUtil.getInstance().getSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        session.save(user);
        
        Query query = session.getNamedQuery("Role.findRoleByName");
        query.setString("roleName", "ROLE_USER");
        Role defaultRole = (Role) query.uniqueResult();
        
        UserRole userRole = new UserRole();
        userRole.setRoleId(defaultRole.getRoleId());
        userRole.setUserId(user.getUserId());
        userRole.setLastUpdateUser("BILLIBWEBAPP");
        session.save(userRole);
        
        session.getTransaction().commit();
    }
    
    
    @Override
    public void updateUser(User user)
    {
        SessionFactory factory = HibernateUtil.getInstance().getSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        session.update(user);
        session.getTransaction().commit();
    }
    
    
    @Override
    public void deleteUser(int userId)
    {
        SessionFactory factory = HibernateUtil.getInstance().getSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        User user = (User) session.get(User.class, userId);
        session.delete(user);
        session.getTransaction().commit();
        
    }
}
